<template>
  <div>
    <el-dialog
      v-model="open"
      :title="title"
      :close-on-click-modal="false"
    >
      <el-form
        ref="editform"
        label-width="130px"
        inline
        :rules="rules"
        :model="form"
      >
        <el-form-item label="柜型" prop="cabinetType">
          <el-select
            v-model="form.cabinetType"
            filterable
            clearable
            style="width: 225px;"
          >
            <el-option
              v-for="dict in dicts[DictDirection.柜子型号]"
              :key="dict.id"
              :label="dict.cabinetType"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="柜子能装实重" prop="totalSolidWeight">
          <el-input
            v-model="form.totalSolidWeight"
           
            clearable
            type="number"
            placeholder="请输入柜子能装实重"
          ></el-input>
        </el-form-item>
        <el-form-item label="柜子能装体积(方)" prop="totalVolumeWeight">
          <el-input
            v-model="form.totalVolumeWeight"
      
            clearable
            placeholder="请输入柜子能装体积(方)"
            type="number"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer> 
        <div>
          <el-button type="primary" @click="submitForm">确定</el-button>
          <el-button @click="cancel">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import { ref, getCurrentInstance, watch, defineExpose, defineEmits } from 'vue'
import { ElMessage } from 'element-plus'
import useDicts, { DictDirection } from '@/store/modules/dicts'
import {
  editData,
  addData,
} from '@/api/BusinessConfig/arkTypeManagement'

const dicts = useDicts()
// 表单参数
const form = ref({
    id: undefined,
    cabinetType: undefined,
    totalSolidWeight: undefined,
    totalVolumeWeight: undefined,
  }),
  // 表单校验
  rules = {
    cabinetType: [
      {
        required: true,
        message: '柜型不能为空',
        trigger: ['blur'],
      },
    ],
    totalSolidWeight: [
      {
        required: true,
        message: '柜子能装实重不能为空',
        trigger: ['blur'],
      },
    ],
    totalVolumeWeight: [
      {
        required: true,
        message: '柜子能装体积(方)不能为空',
        trigger: ['blur'],
      },
    ],
  },
  // 标题
  title = ref(''),
  // 模态框显示
  open = ref(false),
  // 新增修改表单
  editForm = ref(null),
  {
    appContext: {
      config: {
        globalProperties: { $resetForm, $validateForm },
      },
    },
  } = getCurrentInstance()

const emit = defineEmits(['changeList'])
// 重置新增修改列表
const reset = () => {
  form.value = {
    id: undefined,
    cabinetType: undefined,
    totalSolidWeight: undefined,
    totalVolumeWeight: undefined,
  }
  $resetForm(editForm.value)
}
// 新增修改——确定
const submitForm = () => {
  $validateForm(editForm.value, () => {
    let msg = ''
    let response = null
    if (form.value.id != undefined) {
      msg = '修改成功'
      response = editData(form.value)
    } else {
      msg = '新增成功'
      response = addData(form.value)
    }
    response
    .then((res) => {
      if (res.code === 200) {
        ElMessage.success(msg)
        open.value = false
        emit('changeList')
      } else {
        ElMessage.error(res.msg)
      }
    })
    .catch((error) => {
      ElMessage.error(error)
    })
  })
}
// 修改新增——取消
const cancel = () => {
  open.value = false
}

dicts.setDicts(DictDirection.是否禁用字典)
watch(open, (v) => {
  if (!v) {
    reset()
  }
})

defineExpose({
  open,
  title,
  form,
})
</script>
